﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMS
{
    public partial class JY_ClassOpenPlanBLL
    {
        public Message DelClassOpenPlan(int Id)
        {
            DALSession.Current.JY_ClassOpenPlanDAL.DelBy(p => p.ID == Id);
            DALSession.Current.JY_ClassOpenPlanDetailDAL.DelBy(p => p.TotalID == Id);
            DALSession.Current.Savechanges();
            return new Message() { state = "success", msg = "删除成功" };
        }
        public Message DelClassOpenPlanDetail(int Id)
        {
            DALSession.Current.JY_ClassOpenPlanDetailDAL.DelBy(p => p.ID == Id);
            DALSession.Current.Savechanges();
            return new Message() { state = "success", msg = "删除成功" };
        }
        public Message AddClassOpenPlan(int TotalID, JY_ClassOpenPlan ClassOpenPlan1, JY_ClassOpenPlanDetail ClassOpenPlanDetail1)
        {
            Message messg = new Message();
            if (TotalID == 0)
            {
                //新增总表数据
                var list1 = DALSession.Current.JY_ClassOpenPlanDAL.GetListBy(p => p.SchoolCode == OperateContext.sessUserInfo.SchoolCode && p.Year == ClassOpenPlan1.Year);
                if (list1.Count == 0)
                {
                    //总表数据
                    ClassOpenPlan1.SchoolCode = OperateContext.sessUserInfo.SchoolCode;
                    ClassOpenPlan1.SchoolName = OperateContext.sessUserInfo.SchoolName;
                    ClassOpenPlan1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ClassOpenPlan1.EditPerName = OperateContext.sessUserInfo.Name;
                    ClassOpenPlan1.EditPerID = OperateContext.sessUserInfo.Id;
                    ClassOpenPlan1.PlanCode = ClassOpenPlan1.SchoolCode + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + OperateContext.sessUserInfo.Id.ToString();
                    ClassOpenPlan1.ReviewId = PMS.Audit.AddAudit("0502", 0);
                    JY_ClassOpenPlan JYClassOpenPlanBack = DALSession.Current.JY_ClassOpenPlanDAL.AddReturnModel(ClassOpenPlan1);
                    TotalID = Convert.ToInt32(JYClassOpenPlanBack.ID);
                    if (ClassOpenPlanDetail1.DetailNote != "NR")
                    {
                        //明细表数据
                        ClassOpenPlanDetail1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        ClassOpenPlanDetail1.EditPerName = OperateContext.sessUserInfo.Name;
                        ClassOpenPlanDetail1.EditPerID = OperateContext.sessUserInfo.Id;
                        ClassOpenPlanDetail1.TotalID = TotalID;
                        DALSession.Current.JY_ClassOpenPlanDetailDAL.Add(ClassOpenPlanDetail1);
                    }
                    DALSession.Current.Savechanges();

                    messg.state="success";
                    messg.msg="保存成功";
                    messg.returnValue=TotalID.ToString();
                }
                else
                {
                    messg.state = "warning";
                    messg.msg = "该年度信息数据已存在！";
                    messg.returnValue = TotalID.ToString();
                }
            }
            else
            {
                //总表数据
                ClassOpenPlan1.SchoolCode = OperateContext.sessUserInfo.SchoolCode;
                ClassOpenPlan1.SchoolName = OperateContext.sessUserInfo.SchoolName;
                ClassOpenPlan1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ClassOpenPlan1.EditPerName = OperateContext.sessUserInfo.Name;
                ClassOpenPlan1.EditPerID = OperateContext.sessUserInfo.Id;
                DALSession.Current.JY_ClassOpenPlanDAL.ModifyBy(ClassOpenPlan1, p => p.ID == TotalID, "Year", "SchoolYear", "SchoolCode", "SchoolName", "EditPerID", "EditPerName", "EditTime", "Note");
                //明细表数据
                if (ClassOpenPlanDetail1.DetailNote != "NR")
                {
                    ClassOpenPlanDetail1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ClassOpenPlanDetail1.EditPerName = OperateContext.sessUserInfo.Name;
                    ClassOpenPlanDetail1.EditPerID = OperateContext.sessUserInfo.Id;
                    ClassOpenPlanDetail1.TotalID = TotalID;
                    DALSession.Current.JY_ClassOpenPlanDetailDAL.Add(ClassOpenPlanDetail1);
                }
                DALSession.Current.Savechanges();

                messg.state = "success";
                messg.msg = "保存成功";
                messg.returnValue = TotalID.ToString();
            }
            return messg;
        }
        public Message EditClassOpenPlan(int TotalID, JY_ClassOpenPlan ClassOpenPlan1, JY_ClassOpenPlanDetail ClassOpenPlanDetail1)
        {
            Message messg = new Message();
            //总表数据
            ClassOpenPlan1.SchoolCode = OperateContext.sessUserInfo.SchoolCode;
            ClassOpenPlan1.SchoolName = OperateContext.sessUserInfo.SchoolName;
            ClassOpenPlan1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ClassOpenPlan1.EditPerName = OperateContext.sessUserInfo.Name;
            ClassOpenPlan1.EditPerID = OperateContext.sessUserInfo.Id;
            DALSession.Current.JY_ClassOpenPlanDAL.ModifyBy(ClassOpenPlan1, p => p.ID == TotalID, "Year", "SchoolYear", "SchoolCode", "SchoolName", "EditPerID", "EditPerName", "EditTime", "Note");
            //明细表数据
            if (ClassOpenPlanDetail1.DetailNote != "NR")
            {
                ClassOpenPlanDetail1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ClassOpenPlanDetail1.EditPerName = OperateContext.sessUserInfo.Name;
                ClassOpenPlanDetail1.EditPerID = OperateContext.sessUserInfo.Id;
                ClassOpenPlanDetail1.TotalID = TotalID;
                DALSession.Current.JY_ClassOpenPlanDetailDAL.ModifyBy(ClassOpenPlanDetail1, p => p.ID == ClassOpenPlanDetail1.ID, "GradeTypeID", "CourseTypeID", "OpenType", "TgTotalNumSumm", "TgNewNumSumm", "TgRetainNumSumm", "TgPriceSumm", "TgTotalNumAutu", "TgNewNumAutu", "TgRetainNumAutu", "TgPriceAutu", "TgTotalNumWint", "TgNewNumWint", "TgRetainNumWint", "TgPriceWint", "TgTotalNumSpri", "TgNewNumSpri", "TgRetainNumSpri", "TgPriceSpri", "EditPerID", "EditPerName", "EditTime", "DetailNote");
            }
            DALSession.Current.Savechanges();

            messg.state = "success";
            messg.msg = "保存成功";
            messg.returnValue = TotalID.ToString();
            return messg;
        }
    }
}